package com.akita.spring.ai.tools;

import com.akita.spring.ai.entity.po.Course;
import com.akita.spring.ai.entity.po.CourseReservation;
import com.akita.spring.ai.entity.po.School;
import com.akita.spring.ai.entity.query.CourseQuery;
import com.akita.spring.ai.service.ICourseReservationService;
import com.akita.spring.ai.service.ICourseService;
import com.akita.spring.ai.service.ISchoolService;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Component
public class CourseTools {

    @Resource
    private ICourseService iCourseService;

    @Resource
    private ISchoolService iSchoolService;

    @Resource
    private ICourseReservationService iCourseReservationService;

    @Tool(description = "根据条件查询课程")
    public List<Course> queryCourse(@ToolParam(description = "查询的条件", required = false) CourseQuery query) {
        if (Objects.isNull(query)) {
            return Collections.emptyList();
        }
        QueryChainWrapper<Course> wrapper = iCourseService.query()
                .eq(Objects.nonNull(query.getType()), "type", query.getType())
                .le(Objects.nonNull(query.getEdu()), "edu", query.getEdu());
        if (CollectionUtils.isNotEmpty(query.getSorts())) {
            for (CourseQuery.Sort sort : query.getSorts()) {
                wrapper.orderBy(Objects.nonNull(sort.getField()), sort.getAsc(), sort.getField());
            }
        }
        return wrapper.list();
    }

    @Tool(description = "查询所有校区")
    public List<School> querySchool() {
        return iSchoolService.list();
    }

    @Tool(description = "生成预约单，返回预约单ID",name = "课程预约工具")
    public Integer createCourseReservation(@ToolParam(description = "预约课程") String course,
                                           @ToolParam(description = "预约校区") String school,
                                           @ToolParam(description = "学生姓名") String studentName,
                                           @ToolParam(description = "联系电话") String contactInfo,
                                           @ToolParam(description = "备注", required = false) String remark) {
        CourseReservation courseReservation = new CourseReservation();
        courseReservation.setCourse(course);
        courseReservation.setSchool(school);
        courseReservation.setStudentName(studentName);
        courseReservation.setContactInfo(contactInfo);
        courseReservation.setRemark(remark);
        iCourseReservationService.save(courseReservation);
        return courseReservation.getId();
    }

}
